<?php

namespace App\Http\Controllers\Api;

use App\Services\Api\ApiService;
use App\Services\PermissionService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PermissionController extends Controller
{
    private $service;

    public function __construct(ApiService $apiService, PermissionService $service)
    {
        parent::__construct($apiService);
        $this->service = $service;
    }

    /**
     * 查询权限(带分页)
     * @return mixed
     */
    public function index(){
        $showPage = $this->getShowPage();
        if (!empty(request()->input('showpage'))){
            $showPage = request()->input('showpage');
        }
        $data = $this->service->repository->paginate($showPage);
        return $this->apiService->run(200, $data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id){
        $data = $this->service->repository->find($id);
        return $this->apiService->run(200, $data);
    }

    /**
     * 创建权限
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request){
        $role = $this->service->repository->create($request->all());
        return $this->apiService->run(200, $role);
    }

    /**
     * 修改权限
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update(Request $request, $id){
        $role = $this->service->repository->find($id)->update($request->all());
        return $this->apiService->run(200, $role);
    }

    /**
     * 删除权限
     * @param $id
     * @return mixed
     */
    public function destroy($id){
        $this->service->repository->delete($id);
        return $this->apiService->run(200);
    }

}
